package com.hfut.jpoa.action;

import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.hfut.jpoa.common.action.AbsAction;
import com.hfut.jpoa.common.dto.Pagination;
import com.hfut.jpoa.common.dto.ResultDto;
import com.hfut.jpoa.common.util.ConstantUtil;
import com.hfut.jpoa.common.util.DateUtils;
import com.hfut.jpoa.common.util.UID;
import com.hfut.jpoa.dto.UserDTO;
import com.hfut.jpoa.model.Syspara;
import com.hfut.jpoa.model.User;
import com.hfut.jpoa.service.SysparaService;
import com.hfut.jpoa.service.UserService;
import com.hfut.jpoa.service.exception.DispatchException;
import com.hfut.jpoa.service.exception.SysparaException;
import com.hfut.jpoa.service.exception.UserException;

public class UserAction extends AbsAction<UserDTO>
{

	private static final Logger LOGGER = LoggerFactory
	        .getLogger(UserAction.class);

	private UserService userService;

	/**
	 * 用户
	 */
	private User user;

	/**
	 * 标志位：0-增加 1-修改 2-详情
	 */
	private String actionType;

	/**
	 * 
	 */
	private UserDTO userDTO;

	/**
	 * 分页结果集
	 */
	private ResultDto<User> resultDto;

	/**
	 * 总页数
	 */
	private int totalPage;

	/**
	 * 创建者,以后从session中取
	 */
	private String creator = "yl";

	/**
	 * 部门列表
	 */
	private List<Syspara> lstDepart;

	/**
	 * 字典服务
	 */
	private SysparaService sysparaService;

	/**
	 * 序列
	 */
	private static final long serialVersionUID = -4415072662489623867L;

	public String saveUser()
	{
		System.out.println("call useraction's saveUser method");

		if (LOG.isDebugEnabled())
		{
			LOG.debug("save user success!");
		}
		return "index";
	}

	public String queryUser() throws UserException
	{
		User findUser = userService.findUser("aa");
		return findUser.getName();
	}

	/**
	 * 类目管理-类目分页列表
	 * 
	 * @return
	 * @throws CategoryException
	 */
	public String initUser() throws UserException
	{
		// 加载部门
		lstDepart = findLstPath();

		UserDTO userDTO = new UserDTO();
		Pagination pagination = new Pagination(0);
		userDTO.setPagination(pagination);
		resultDto = userService.queryUserInfoForPage(userDTO);
		totalPage = resultDto.getPagination().getTotalPage();
		return "user_list_init";
	}

	/**
	 * 用户列表显示数据
	 * 
	 * @return
	 * @throws UserException
	 */
	public String list() throws UserException
	{
		HttpServletRequest request = ServletActionContext.getRequest();
		String currentPage = request.getParameter("currentPage");
		Pagination pagination = new Pagination(Integer.parseInt(currentPage));
		String pagesize = request.getParameter("pageSize");
		pagination.setPageSize(Integer.parseInt(pagesize));
		if (userDTO == null)
		{
			userDTO = new UserDTO();
		}
		userDTO.setPagination(pagination);
		resultDto = userService.list(userDTO);
		totalPage = resultDto.getPagination().getTotalPage();

		return "list";
	}

	/**
	 * 加载用户弹出层
	 * 
	 * @return
	 * @throws CategoryException
	 */
	public String initDialog() throws UserException
	{
		// 加载部门
		lstDepart = findLstPath();
		
		UserDTO userDTO = new UserDTO();
		Pagination pagination = new Pagination(0);
		userDTO.setPagination(pagination);
		resultDto = userService.queryUserInfoForPage(userDTO);
		totalPage = resultDto.getPagination().getTotalPage();
		return "user_dialog_init";
	}

	/**
	 * 加载用户弹出层列表
	 * 
	 * @return
	 * @throws UserException
	 */
	public String listDialog() throws UserException
	{

		HttpServletRequest request = ServletActionContext.getRequest();
		String currentPage = request.getParameter("currentPage");
		Pagination pagination = new Pagination(Integer.parseInt(currentPage));
		String pagesize = request.getParameter("pageSize");
		pagination.setPageSize(Integer.parseInt(pagesize));
		if (userDTO == null)
		{
			userDTO = new UserDTO();
		}
		userDTO.setPagination(pagination);
		resultDto = userService.list(userDTO);
		totalPage = resultDto.getPagination().getTotalPage();

		return "user_dialog_list";
	}

	public void createUserCode() throws UserException
	{
		String userCode = userService.createUserCode(user);
		try
		{
			success(userCode);
		} catch (IOException e)
		{
			LOGGER.error("自动生成用户编码异常", e);
		}
	}

	/**
	 * 验证编码是否存在
	 * 
	 * @throws IOException
	 */
	public void codeValidator() throws IOException
	{
		boolean flag = false;
		try
		{
			flag = userService.codeValidator(user);
		} catch (UserException e)
		{
			LOGGER.error("验证编码是否存在异常", e);
			failure("验证编码是否存在异常" + e.getMessage());
		}
		if (flag)
		{
			failure("该编码已存在，请重新输入");
		} else
		{
			success("输入正确，该编码可以使用");
		}
	}

	/**
	 * 加载部门列表
	 * 
	 * @return
	 * @throws DispatchException
	 */
	private List<Syspara> findLstPath() throws UserException
	{
		// 从字典表中加载路线
		List<Syspara> lstPathTemp = new ArrayList<Syspara>();
		try
		{
			Syspara syspara = new Syspara();
			syspara.setType(ConstantUtil.DEPARTMENT);
			lstPathTemp = sysparaService.querySysparaList(syspara);
		} catch (SysparaException e)
		{
			LOGGER.error("初始化部门列表异常异常：", e);
			throw new UserException("初始化部门列表异常异常：", e);
		}
		return lstPathTemp;
	}

	/**
	 * 增加页面初始化
	 */
	public String userAddInit() throws UserException
	{
		// 加载部门
		lstDepart = findLstPath();

		actionType = ConstantUtil.ACTIONTYPE_ADD;
		return "user_add_init";
	}

	/**
	 * 增加用户
	 * 
	 * @return
	 * @throws UserException
	 */
	public String addUser() throws UserException
	{
		if (null == user)
		{
			throw new UserException("增加用户异常");
		}
		try
		{
			user.setCreatDate(DateUtils.getNowDate());
		} catch (ParseException e)
		{
			LOGGER.error("增加用户异常", e);
		}
		user.setId(UID.getID());
		user.setCreator(creator);
		userService.saveUser(user);
		return "user_list_init";
	}

	/**
	 * 修改页面初始化
	 */
	public String userUpdateInit() throws UserException
	{
		actionType = ConstantUtil.ACTIONTYPE_UPDATE;
		User resultUser = userService.findUserInfo(user);
		user = resultUser;
		return "user_add_init";
	}

	/**
	 * 修改用户
	 * 
	 * @return
	 * @throws UserException
	 */
	public String updateUser() throws UserException
	{
		userService.updateUser(user);
		return "user_list_init";
	}

	public void setUserService(UserService userService)
	{
		this.userService = userService;
	}

	public User getUser()
	{
		return user;
	}

	public void setUser(User user)
	{
		this.user = user;
	}

	public UserDTO getUserDTO()
	{
		return userDTO;
	}

	public void setUserDTO(UserDTO userDTO)
	{
		this.userDTO = userDTO;
	}

	public ResultDto<User> getResultDto()
	{
		return resultDto;
	}

	public void setResultDto(ResultDto<User> resultDto)
	{
		this.resultDto = resultDto;
	}

	public int getTotalPage()
	{
		return totalPage;
	}

	public void setTotalPage(int totalPage)
	{
		this.totalPage = totalPage;
	}

	public String getCreator()
	{
		return creator;
	}

	public void setCreator(String creator)
	{
		this.creator = creator;
	}

	public String getActionType()
	{
		return actionType;
	}

	public void setActionType(String actionType)
	{
		this.actionType = actionType;
	}

	public List<Syspara> getLstDepart()
	{
		return lstDepart;
	}

	public void setLstDepart(List<Syspara> lstDepart)
	{
		this.lstDepart = lstDepart;
	}

	public SysparaService getSysparaService()
	{
		return sysparaService;
	}

	public void setSysparaService(SysparaService sysparaService)
	{
		this.sysparaService = sysparaService;
	}

}
